Repository navigation
feat: accept std::string and std::string_view as keys in operator[] - #6316
Conversation
Assisted-by: ClaudeCode:claude-fable-5-2
Assisted-by: ClaudeCode:claude-opus-5-5
|
— codex on behalf of Ralf — No actionable code findings. The overload is constrained to exact The abi3t-on-GIL job failed during configure with HTTP 504 downloading Catch2. The targeted rerun passed. Rerun job |
|
to be clear, no need to merge this if #6286 can be merged by 3.2.0 release. |
I'm still opposed to merging 6286:
The superficial convenience afforded by 6286 is very minor and at the same time a trap. It makes expensive repeated to-Python conversions much harder to pin-point, certainly for humans, and I believe even for agents (one more mental hoop to jump through). Requiring Requiring Not requiring |
There was a problem hiding this comment.
codex GPT-6.2-Sol ultra (I pointed it at my previous comment):
No actionable findings at commit 20fb21e. The overload is constrained to std::string and std::string_view, which preserves existing overload resolution. It uses the length-aware py::str conversion and stores the resulting Python key in the accessor, so embedded NULs and temporary views are handled correctly. PR #6316 · Implementation
Your concern about #6286 applies even to valid, hashable keys. A std::pair<std::string, std::string> would rebuild a Python tuple and convert both strings on every lookup. An explicit py::cast(key) before the loop makes that cost visible and lets callers reuse the result. #6316 still converts strings on each use, but confines the new implicit conversion to those two types. Your comment · #6286 conversion path
|
I still think the AI is stuck on a non-issue. If you have a tuple of values, building a tuple isn't that costly, you have to rebuild it if any values are not constant, and if the whole thing is constant, why not cache the value you get out instead of caching the key you put in? That's even faster! Lots of other places in pybind11 automatically convert, that's why pybind11 is easy to use. I don't think dict keys are special. The time saved by having it just work out of the box is likely better than the time wasted when it fails to compile and you have to add If pybind11 wasn't designed around automatic conversion, or if this is rust, it might be different, but it's not how the rest of the library was designed. But fine. |
|
I agree the But this got me to ask another question:
This is what codex pulled out: Yes. A callback registry is a plausible case. With const std::function<int(int)> callback = [offset = 42](int x) { return x + offset; };
py::dict d;
d[callback] = 1;
py::object value = d[callback]; // KeyError with #6286A second assignment would create a second entry. Casting once with A copyable The same issue already exists with I think such things will be rare, but I've actually stumbled over corner cases like that in real life a few times. |
I prefer #6286. I don't buy the argument that dict keys are special vs. any other conversion; objects that cannot be reconverted or have really expensive conversions are very bad dict keys. If you are doing something weird and making dict keys that can't be reconverted, you should know what you are doing.
But if we are on this one at a time,
std::stringandstd::string_vieware probably the most important one afterconst char *.🤖 AI text below 🤖
Description
Follow-up to #6291, requested in #6286 (comment).
obj[key]now accepts astd::stringor astd::string_view(C++17) as the key, for exampled[std::string("s")],d[std::string_view("s")]ando[key][other_key]. The key is built withpy::str, so embedded NUL characters are kept.The change adds one constrained template overload of
object_api::operator[]. It matches onlystd::stringandstd::string_view, through a newdetail::is_string_keytrait. A plainconst std::string &overload would also take part in overload resolution for types that have an implicit conversion tostd::string, and could make calls that compile today ambiguous. Thehandle,object &&,const char *and integer overloads are unchanged.Other key types still need an explicit
py::str(key)orpy::cast(key).Suggested changelog entry:
operator[]onpy::object,py::dictand accessors now accepts astd::stringorstd::string_viewas the key.